package com.test.uiapp2.function.Settings;

import android.database.sqlite.SQLiteException;
import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Settings.SettingsWlanElement;

import javax.security.auth.login.LoginException;

public class SettingsWlanFunction extends TestBase {

    //关闭飞行模式
    public void FlightMode_stop(){
        try {
            startSettings();
            UiObject FMswitch = getId(SettingsWlanElement.FMswitch_widget);
            if(FMswitch.exists()){
                FMswitch.click();
                sleep(WAITTIME);
                String wifistatus = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCHWIDGET).getText();
                Log.i(TAG, "WIFI当前状态为="+wifistatus);
                verify("关闭飞行模式,wifi未关闭",wifistatus.equals(SettingsWlanElement.START));
                sleep(WAIT);
                swipe_right();
                sleep(HWAIT);
                minimize();
            }else{
                Log.i(TAG, "settings: 未进入飞行模式设置界面");
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //开启飞行模式
    public  void FlightMode_start(){
        try {
            startSettings();
            UiObject FMswitch = getId(SettingsWlanElement.FMswitch_widget);
            if(FMswitch.exists()){
                FMswitch.click();
                sleep(WAITTIME);
                String wifistatus = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCHWIDGET).getText();
                Log.i(TAG, "WIFI当前状态为="+wifistatus);
                verify("开启飞行模式,wifi未关闭",wifistatus.equals(SettingsWlanElement.CLOSE));
                sleep(WAIT);
                swipe_right();
                sleep(HWAIT);
                minimize();
            }else{
                Log.i(TAG, "settings: 未进入飞行模式设置界面");
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
    //StatusBar-wifi关闭
    public void StatusBar_stop(){
        try {
            downStatusBar();
            sleep(WAIT);
            UiObject className = getClassName(SettingsWlanElement.VIEWGROUP);
            UiObject child = className.getChild(new UiSelector().className(SettingsWlanElement.SWITCH).index(0));
            if(child.exists()){
                String text = child.getText();
                if(text.equals(SettingsWlanElement.CLOSE)){
                    Log.i(TAG, "wifistatus = wifi当前为关闭状态");
                    child.click();
                    sleep(WAITTIME);
                    String wifistatus = child.getText();
                    Log.i(TAG, "打开后wifi状态为="+wifistatus);
                    verify("打开wifi失败",wifistatus.equals(SettingsWlanElement.START));
                    upwardStatusBar();
                }else{
                    Log.i(TAG, "wifistatus = wifi当前为打开状态");
                    child.click();
                    sleep(WAITTIME);
                    String wifistatus = child.getText();
                    Log.i(TAG, "关闭后wifi状态为="+wifistatus);
                    verify("打开wifi失败",wifistatus.equals(SettingsWlanElement.CLOSE));
                    upwardStatusBar();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //StatusBar-wifi打开
    public void StatusBar_start(){
        try {
            downStatusBar();
            sleep(WAIT);
            UiObject className = getClassName(SettingsWlanElement.VIEWGROUP);
            UiObject child = className.getChild(new UiSelector().className(SettingsWlanElement.SWITCH).index(0));
            if(child.exists()){
                String text = child.getText();
                if(text.equals(SettingsWlanElement.CLOSE)){
                    Log.i(TAG, "wifistatus = wifi当前为关闭状态");
                    child.click();
                    sleep(WAITTIME);
                    String wifistatus = child.getText();
                    Log.i(TAG, "打开后wifi状态为="+wifistatus);
                    verify("打开wifi失败",wifistatus.equals(SettingsWlanElement.START));
                    upwardStatusBar();
                }else{
                    Log.i(TAG, "wifistatus = wifi当前为打开状态");
                    child.click();
                    sleep(WAITTIME);
                    String wifistatus = child.getText();
                    Log.i(TAG, "关闭后wifi状态为="+wifistatus);
                    verify("打开wifi失败",wifistatus.equals(SettingsWlanElement.CLOSE));
                    upwardStatusBar();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //连接Jingos wifi
    public void Wlan_Connect(){
        try {
            startSettings();
            if (getTest(SettingsWlanElement.WLANNAME).exists()){
                clickByText(SettingsWlanElement.WLAN);
                sleep(WAIT);
                if(getId(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET).exists()){
                    String status = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCH_WIDGET).getText();
                    if(status.equals(SettingsWlanElement.CLOSE)){
                        clickById(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET);
                        sleep(WAITTIME);
                    }
                    ConnectJingoswifi();
                }
            }else{
                Log.i(TAG, "settings: 未进入wifi界面");
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }

    }
    //断开Jingos连接wifi
    public void Wlan_DisConnect(){
        startSettings();
        if (getTest(SettingsWlanElement.WLANNAME).exists()){
            clickByText(SettingsWlanElement.WLAN);
            sleep(WAIT);
            UiObject JingOSwifi = getTest(SettingsWlanElement.JINGOSWLAN);
            if(JingOSwifi.exists()) {
                clickByText(SettingsWlanElement.JINGOSWLAN);
                sleep(WAITTIME);
                clickByText(SettingsWlanElement.CLEAN);
                verify("断开wifi失败",!getTest(SettingsWlanElement.CONNECTED).exists());
                sleep(WAIT);
                swipe_right();

            }else{
                verify("没有找到JingOS wifi",JingOSwifi.exists());
            }

        }
    }


    //网络连接成功后-浏览网页
    public  void  Wlan_BrowseWebPage(){

    }

    //关闭wifi连接
    public void  Wlan_Stop(){
        try {
            startSettings();
            if (getTest(SettingsWlanElement.WLANNAME).exists()){
                clickByText(SettingsWlanElement.WLAN);
                sleep(WAIT);
                if(getId(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET).exists()){
                    //获取点击前wifi的状态
                    String Beforestatus = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCH_WIDGET).getText();
                    if(Beforestatus.contains(SettingsWlanElement.CLOSE)){
                        Log.i(TAG, "点击前wifi状态为："+Beforestatus);
                        Log.i(TAG, "先打开在关闭");
                        clickById(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET);
                        sleep(WAITTIME);
                    }
                    clickById(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET);
                    String status = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCH_WIDGET).getText();
                    Log.i(TAG, "点击后wifi状态为:"+status);
                    sleep(SHOTWAIT*2);
                    verify("未关闭wifi连接",status.equals(SettingsWlanElement.CLOSE));
                    sleep(SHOTWAIT);
                    Wlan_Start();

                }
            }else{
                Log.i(TAG, "settings: 未进入wifi界面");
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
   //开启wifi连接
    public  void  Wlan_Start(){
        try {
            startSettings();
            if (getTest(SettingsWlanElement.WLANNAME).exists()){
                clickByText(SettingsWlanElement.WLAN);
                sleep(WAIT);
                if(getId(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET).exists()){
                    //获取点击前wifi的状态
                    String Beforestatus = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCH_WIDGET).getText();
                    if(Beforestatus.contains(SettingsWlanElement.START)){
                        Log.i(TAG, "点击前wifi状态为："+Beforestatus);
                        Log.i(TAG, "先关闭在打开");
                        clickById(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET);
                        sleep(WAITTIME);
                    }
                    clickById(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET);
                    String Afterstatus = getId(BaseElement.SETTINGS, SettingsWlanElement.SWITCH_WIDGET).getText();
                    Log.i(TAG, "点击后wifi状态为:"+Afterstatus);
                    sleep(SHOTWAIT*2);
                    verify("未开启wifi连接",Afterstatus.equals(SettingsWlanElement.START));
                    sleep(WAIT);
                    for (int i=0;i<3;i++){
                        sleep(WAIT);
                        swipe_right();
                    }
                }else{
                    verify("未进入wifi设置界面",getId(BaseElement.SETTINGS,SettingsWlanElement.SWITCH_WIDGET).exists());
                }
            }else{
                verify("未进入设置界面",getTest(SettingsWlanElement.WLANNAME).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
    //打开wifi设置公用方法
    public void startSettings(){
        try {
            startApp(SettingsWlanElement.WLANNAME);
            sleep(WAITTIME);
            uiDevice.swipe(576,463,556,1146,STEP);
            if(getTest(SettingsWlanElement.WLANNAME).exists()){
                clickByText(SettingsWlanElement.WLANNAME);
                sleep(WAIT);
            }else{
//                verify("未进入设置界面",getTest(SettingsWlanElement.WLANNAME).exists());
                verify("未进入设置界面",getTest(SettingsWlanElement.SETTINGVIEW).exists());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //连接wifi子方法
    public void ConnectJingoswifi(){
        try {
            UiObject JingOSwifi = getTest(SettingsWlanElement.JINGOSWLAN);
            if(JingOSwifi.exists()){
                clickByText(SettingsWlanElement.JINGOSWLAN);
                getId(BaseElement.SETTINGS,SettingsWlanElement.PASSWORD).setText(SettingsWlanElement.PASSWORDNAME);
                sleep(WAIT);
                clickByText(SettingsWlanElement.CONNECT);
                sleep(MWAITTIME);
                verify("未连接成功",getTest(SettingsWlanElement.CONNECTED).exists());
            }else{
                verify("没有找到JingOS wifi",JingOSwifi.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //向下滑出通知栏
    public void downStatusBar(){
        sleep(WAIT);
        uiDevice.swipe(506,15,574,791,STEP);
    }
    //向上滑动关闭通知栏
    public void upwardStatusBar(){
        sleep(WAIT);
        uiDevice.swipe(593,1712,640,685,STEP);
    }
}
